package edu.biwu.dao.impl;

import edu.biwu.dao.StudentDao;
import edu.biwu.po.Student;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 通过构造方法初始化sqlSessionFactory
     * @param sqlSessionFactory
     */
    public StudentDaoImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public Student findBySno(int sno) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            return sqlSession.selectOne("test.findBySno",sno);
        }finally {
            sqlSession.close();
        }

    }

    @Override
    public List<Student> findBySname(String sname) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            return sqlSession.selectList("test.findBySname",sname);
        }finally {
            sqlSession.close();
        }
    }
}
